﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace Twi.COMMON.WPF
{
    public enum RequestResult 
    {
        CommunicationFailure = 0 //通讯异常故障
        , ServerException = 3 //服务器端未处理异常
        , ServerExceptionCatched = 31 //捕获的异常，客户端用messagebox显示
        , ServerResultSuccess = 1 //无异常，达到预期目的
        , ServerResultUnsuccess = 2 //无异常，由于业务逻辑的原因，没有达到预期目的
        , ServerResultUnsuccessOKCancel = 4 //无异常，由于业务逻辑的原因，没有达到预期目的，选择是否进行二次操作
        ,
        ServerResultUnsuccessOK = 5  //无异常，由于业务逻辑的原因，没有达到预期目的，OK
        , ServerResultUnsuccessCancel = 6  //无异常，由于业务逻辑的原因，没有达到预期目的，NO
        , ServerResultUnsuccessYesNo = 7
        , ServerResultUnsuccessYes = 8
        , ServerResultUnsuccessNo = 9
        , ClientVersionException = 99 //客户端版本异常

    }
    
    public class ResponseMessageInfo
    {
        public ResponseMessageInfo(XElement xeDataRoot) 
        {
            _requestResult = (RequestResult)int.Parse(xeDataRoot.Element("ResponseStatus").Element("Status").Value);
            _message = xeDataRoot.Element("ResponseStatus").Element("Message").Value;
            _exception = xeDataRoot.Element("ResponseStatus").Element("Exception").Value;            
            _responseData = xeDataRoot.Elements().Where(x => x.Name.LocalName == "ResponseData").SingleOrDefault();
        }

        private RequestResult _requestResult;
        public RequestResult Status { get { return _requestResult; } }
        private string _message;
        public string Message { get { return _message; } }
        private string _exception;

        /// <summary>
        /// 异常
        /// </summary>
        public string Exception { get { return _exception; } }
        private XElement _responseData;

        /// <summary>
        /// 主体（列表/单个/详细）
        /// </summary>
        public XElement ResponseDataMainBody
        {
            get 
            {
                if (_responseData!=null && _responseData.HasElements)
                {
                    return _responseData.Elements().Where(x => !new string[] { "PageAmount", "ItemAmount" }.Contains(x.Name.LocalName)).SingleOrDefault();
                }
                else
                {
                    return null;
                }               
            }
        }

        /// <summary>
        /// 总页数
        /// </summary>
        public int PageAmount 
        {
            get 
            {
                return
                    int.Parse(_responseData.Element("PageAmount").Value);
            }
        }

        /// <summary>
        /// 总记录数
        /// </summary>
        public int ItemAmount
        {
            get
            {
                return
                    int.Parse(_responseData.Element("ItemAmount").Value);
            }
        }
    }
}
